// useUsersModal

import { getProductCategoryList } from "@/services/product";
import { useEffect, useState } from "react";

export const useCategoryModel = () => {
  const [loading, setLoading] = useState<boolean>(false);
  const [categoryList, setCategoryList] = useState<any[]>([]);
  const getData = async () => {
    setLoading(true);
    const { data } = await getProductCategoryList({});

    const resetTree = (data: any, parentId?: undefined) => {
      return data?.map((item: any) => ({
        title: item.categoryName,
        value: item.id,
        parentId,
        children: resetTree(item.subList || [], item.id),
      }));
    };

    const formattedTree = resetTree(data);
    setCategoryList(formattedTree);

    setLoading(false);
  };

  useEffect(() => {
    getData();
  }, []);
  //刷新
  const refresh = () => {
    getData();
  };
  return {
    loading,
    categoryList,
    refresh,
  };
};
